package com.syq.play.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import com.syq.play.api.SongApi;
import com.syq.play.db.entity.SongEntity;
import com.syq.play.db.entity.SongManager;
import com.syq.play.model.MediaModel;
/**
 * 歌曲文件接口实现类
 * @author ShiYiqiong
 *
 */
public class SongImpl implements SongApi{
	private static final String TAG="SongImpl";
	private Context context;
	private static final String MIN_TIME="10000";
	
	/**
	 * 将系统歌曲数据插入到歌曲数据表
	 */
	public void insertAllSong() {
		List<HashMap<String, Object>> list=new ArrayList<HashMap<String,Object>>();
		String selection=MediaStore.Audio.Media.DURATION+">=?";
		String[] selectionArgs=new String[] { MIN_TIME };
		ContentResolver cr=context.getContentResolver();
		Cursor cursor = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, selection, selectionArgs, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		if(cursor.moveToFirst()){
			do{
				int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
				String displayName=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
				String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
				String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
				String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
				String data = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
				String folder=data.substring(0, data.lastIndexOf("/"));
				int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
				long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
				HashMap<String, Object> item=new HashMap<String, Object>();
				item.put(SongEntity.AUDIO_ID, id);
				item.put(SongEntity.DISPLAY_NAME, displayName);
				item.put(SongEntity.TITLE, title);
				item.put(SongEntity.ALBUM, album);
				item.put(SongEntity.ARTIST, artist);
				item.put(SongEntity.FOLDER, folder);
				item.put(SongEntity.DATA, data);
				item.put(SongEntity.DURATION, duration);
				item.put(SongEntity.SIZE, size);
				list.add(item);
			}while(cursor.moveToNext());
		}
		SongManager.getInstance().insertAllSongData(list);
	}
	/**
	 * 获取所有歌曲
	 */
	public List<MediaModel> getAllSong() {
		return SongManager.getInstance().getAllSong();
	}
	public List<MediaModel> getAllSongFromArtist(String artist) {
		return SongManager.getInstance().getMediaFromKey(SongEntity.ARTIST, artist);
	}
	public List<MediaModel> getAllSongFromAlbum(String album) {
		return SongManager.getInstance().getMediaFromKey(SongEntity.ALBUM, album);
	}
	public List<MediaModel> getAllSongFromFolder(String folder) {
		return SongManager.getInstance().getMediaFromKey(SongEntity.FOLDER, folder);
	}
	/**
	 * 获取所有最爱歌曲
	 */
	public List<MediaModel> getAllFavoriteSong() {
		return SongManager.getInstance().getAllFavoriteSong();
	}
	/**
	 * 获取所有最近播放歌曲
	 */
	public List<MediaModel> getAllRecentSong() {
		return SongManager.getInstance().getAllRecentSong();
	}
	/**
	 * 采用单例模式，封装构造方法
	 */
	private SongImpl(){
	}
	/**
	 * 设置context
	 * @param context
	 */
	private void setContext(Context context){
		this.context=context;
	}
	/**
	 * 类级内部类，在首次被访问时才会被初始化
	 */
	private static class SingleHolder {
		static void setContext(Context context) {
			objsingle.setContext(context);
		}

		/**
		 * 静态初始化器，有JVM来保证线程安全
		 */
		private static SongImpl objsingle = new SongImpl();
	}

	/**
	 * 单例，获得接口实现
	 * 
	 * @return
	 */
	public static SongImpl getInstance(Context context) {
		SingleHolder.setContext(context);
		return SingleHolder.objsingle;
	}
}
